package com.laotou99.wmsjjj.wmsjjj.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.laotou99.wmsjjj.wmsjjj.bean.po.CUSTPO;
import com.laotou99.wmsjjj.wmsjjj.bean.vo.CUSTVO;
import com.laotou99.wmsjjj.wmsjjj.repository.CUSTPORepository;

@Service
public class CUSTService {

	@Autowired
	CUSTPORepository custpoRepository;
	
	public boolean saveCUST(CUSTVO custvo) 
	{		
		if(checkStorekey(custvo)) {
			return true;
		}		
		CUSTPO po1 = new CUSTPO(); 
		BeanUtils.copyProperties(custvo, po1); 
		po1.setCreateDate(new Date());
		
		CUSTPO po2 = custpoRepository.save(po1); 
		System.out.println("saveCUST po2-=>"
			+JSON.toJSONString(po2)); 
		
		return false;
	}

	private boolean checkStorekey(CUSTVO custvo) 
	{ 
		String str1 = custvo.getStorekey();
		if(str1==null || str1.equals("")) {
			return true;
		}		
		CUSTPO custpo1 = custpoRepository.findByStorekey(str1);
		if(custpo1==null){}else {
			System.out.println(""+JSON.toJSONString(custpo1));
			return true;
		}
		return false;
	}

	public boolean updateCUST(CUSTVO custvo) {
		if(check_CustId(custvo.getId())) {
			return true;
		}
		CUSTPO po1 = new CUSTPO();
		BeanUtils.copyProperties(custvo, po1);
		CUSTPO po2 = custpoRepository.save(po1);
		System.out.println("updateCUST po2-=>"
			+JSON.toJSONString(po2));
		
		return false;
	}

	private boolean check_CustId(long id) {		
		CUSTPO custpo1 = custpoRepository.findOne(id);		
		if(custpo1==null) {
			return true;
		}else {} 
		return false;
	}

	public List<CUSTVO> showAllCUST() {
		List<CUSTVO> li1 = new ArrayList<CUSTVO>();
		List<CUSTPO> custpoList = custpoRepository.findAll();
		for(CUSTPO po:custpoList) {
			CUSTVO vo1 = new CUSTVO();
			BeanUtils.copyProperties(po, vo1);
			li1.add(vo1);
		} 
		return li1;
	}

	public boolean deleteCUST(long l1) {
		custpoRepository.delete(l1);
		return false;
	}

	
	
}
